package com.ruoyi.power.service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.GsonUtils;
import com.ruoyi.common.utils.HttpUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.baidu.Util;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.power.Vo.Response;
import com.ruoyi.power.bangongwendang.BgwdBack;
import com.ruoyi.power.bangongwendang.BgwdBackRet;
import com.ruoyi.power.shouxieshibie.SxsbBack;
import com.ruoyi.power.shouxieshibie.SxsbBackRet;
import com.ruoyi.power.tongyong.TyBack;
import com.ruoyi.power.tongyong.TyBackRet;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RequestService {
    private static final Logger log = LoggerFactory.getLogger(RequestService.class);

    public static AjaxResult gettongyong(String url,MultipartFile file) {
        // 请求url, 注意替换实际请求的IP和端口
        try {
            String args = "object_type=general_v5&disp_line_poly=true&type=st_ocrapi&languagetype=CHN_ENG&eng_granularity=word&imgDirection=setImgDirFlag";
            // 读取resource中的图片文件
            byte[] content = Util.readFileByBytes(file);
            // 图片byte[] 直接base64
            String imageB64 = Base64.getEncoder().encodeToString(content);
            // 拼接参数
            args = args + "&image=" + imageB64;

            // 构造外层的json 参数字符串
            Map<String, String> map = new HashMap<String, String>();
            map.put("appid", "123456");
            map.put("logid", "123456");
            map.put("format", "json");
            map.put("from", "test-java");
            map.put("clientip", "0.0.0.0");
            map.put("data", Base64.getEncoder().encodeToString(args.getBytes()));
            String param = GsonUtils.toJson(map);

            // 发送请求
            String result = HttpUtil.postGeneralUrl(url, "application/json", param, "UTF-8");
            System.out.println(result);

            // 返回字符串映射到model类
            Response resp = new Response();
            resp = GsonUtils.fromJson(result, resp.getClass());

            // 返回结果decode
            String str = new String(Base64.getDecoder().decode(resp.result.getBytes()));
            log.info("gettongyong - {}", str);
            System.out.println(str);
            TyBack backEntity = JSONObject.parseObject(str, TyBack.class);
            if (backEntity != null && backEntity.getRet() != null) {
                return AjaxResult.success(backEntity.getRet());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AjaxResult getshouxieshibie(String url,MultipartFile file) {
        // 请求url, 注意替换实际请求的IP和端口
        try {
            String args = "object_type=general_handwriting&type=st_ocrapi&eng_granularity=word&disp_line_poly=true";
            // 读取resource中的图片文件
            byte[] content = Util.readFileByBytes(file);
            // 图片byte[] 直接base64
            String imageB64 = Base64.getEncoder().encodeToString(content);
            // 拼接参数
            args = args + "&image=" + imageB64;

            // 构造外层的json 参数字符串
            Map<String, String> map = new HashMap<String, String>();
            map.put("appid", "123457");
            map.put("logid", "123457");
            map.put("format", "json");
            map.put("from", "test-java");
            map.put("clientip", "0.0.0.0");
            map.put("data", Base64.getEncoder().encodeToString(args.getBytes()));
            String param = GsonUtils.toJson(map);

            // 发送请求
            String result = HttpUtil.postGeneralUrl(url, "application/json", param, "UTF-8");
            System.out.println(result);

            // 返回字符串映射到model类
            Response resp = new Response();
            resp = GsonUtils.fromJson(result, resp.getClass());

            // 返回结果decode
            String str = new String(Base64.getDecoder().decode(resp.result.getBytes()));
            log.info("getshouxieshibie - {}", str);
            System.out.println(str);
            SxsbBack backEntity = JSONObject.parseObject(str, SxsbBack.class);
            if (backEntity != null && backEntity.getRet() != null) {
                return AjaxResult.success(backEntity.getRet());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AjaxResult getbangongwendang(String url,MultipartFile file) {
        // 请求url, 注意替换实际请求的IP和端口
        try {
            String args = "object_type=general_v4_officedoc&recog_type=handprint_mix&type=st_ocrapi&languagetype=CHN_ENG&detecttype=LocateRecognize&disp_line_poly=true&disp_layout_analysis=true&recg_tables=true&erase_seal=false&imgDirection=setImgDirFlag&disp_underline_analysis=true&recg_formula=true&disp_combine_res=true&disp_formula_res=true&simplify_latex=true&line_probability=true";
            String extension = FileUploadUtils.getExtension(file);
            if(StringUtils.isNotEmpty(extension) &&extension.toLowerCase().equals("pdf")){
                args=args+"&file_type=pdf&file_page_number=[0, n)";
            }
            // 读取resource中的图片文件
            byte[] content = Util.readFileByBytes(file);
            // 图片byte[] 直接base64
            String imageB64 = Base64.getEncoder().encodeToString(content);
            // 拼接参数
            args = args + "&image=" + imageB64;

            // 构造外层的json 参数字符串
            Map<String, String> map = new HashMap<String, String>();
            map.put("appid", "123458");
            map.put("logid", "123456");
            map.put("format", "json");
            map.put("from", "test-java");
            map.put("clientip", "0.0.0.0");
            map.put("data", Base64.getEncoder().encodeToString(args.getBytes()));
            String param = GsonUtils.toJson(map);

            // 发送请求
            String result = HttpUtil.postGeneralUrl(url, "application/json", param, "UTF-8");
            System.out.println(result);

            // 返回字符串映射到model类
            Response resp = new Response();
            resp = GsonUtils.fromJson(result, resp.getClass());

            // 返回结果decode
            String str = new String(Base64.getDecoder().decode(resp.result.getBytes()));

            log.info("getbangongwendang - {}", str);
            System.out.println(str);
            BgwdBack backEntity = JSONObject.parseObject(str, BgwdBack.class);
            if (backEntity != null && backEntity.getRet() != null) {
                return AjaxResult.success(backEntity.getRet());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
